package main

import (
	"context"
	"fmt"
	"gitee.com/scottq/go-framework/grpcProtos/pb"
	v1config "gitee.com/scottq/go-framework/src/v1/config"
	v1clientrpc "gitee.com/scottq/go-framework/src/v1/libs/clients/grpc"
	v1log "gitee.com/scottq/go-framework/src/v1/log"
	"google.golang.org/grpc"
	"os"
	"path/filepath"
)

func main() {

	logPath := fmt.Sprintf("./runtime/logs/%s.log", filepath.Base(os.Args[0]))
	logger := v1log.NewZapLog("example", logPath, nil)

	c := v1config.RpcClientConfig{
		RpcServer: "127.0.0.1:40001", // RpcDisWay=none 时直连rpc
		RpcDisWay: "none",
		//RpcDisWay:    "etcd",
		RpcDisServer: "127.0.0.1:2379",
	}

	opts := []v1clientrpc.RemoteOption{}

	switch c.RpcDisWay {
	case v1config.RpcRegByEtcd:
		opts = append(opts,
			v1clientrpc.OptEtcdDiscovery(c.RpcDisServer),
			v1clientrpc.OptConnTimeout(c.RpcTimeout))
	}

	conn, err := v1clientrpc.NewRemoteConn("admin", c.RpcServer, opts...)
	if err != nil {
		logger.Error("new remote conn err:%s", err)
		return
	}
	demo1(conn, logger)
	demo2(conn, logger)
}

func demo1(conn *grpc.ClientConn, logger v1log.ILog) {
	//打印连接的状态信息
	info := conn.GetState()
	logger.Info(info.String())

	client := NewDemoClient(conn)

	resp, err := client.CheckAuth("12334")
	if err != nil {
		logger.Error("fail:%s", err)
		return
	}
	logger.Info("resp:%v", resp)
}

func demo2(conn *grpc.ClientConn, logger v1log.ILog) {
	var err error
	//打印连接的状态信息
	info := conn.GetState()
	logger.Info(info.String())

	client := NewDemoClient(conn)

	resp, err := client.SetSetting("count", "123")
	if err != nil {
		logger.Error("fail:%s", err)
		return
	}
	logger.Info("resp:%v", resp)

	resp, err = client.GetSetting("count")
	if err != nil {
		logger.Error("fail:%s", err)
		return
	}
	logger.Info("resp:%v", resp)
}

type DemoClient struct {
	remoteUser    pb.UserSrClient
	remoteSetting pb.SettingSrClient
}

func NewDemoClient(conn *grpc.ClientConn) *DemoClient {
	return &DemoClient{
		remoteUser:    pb.NewUserSrClient(conn),
		remoteSetting: pb.NewSettingSrClient(conn),
	}
}

func (sr *DemoClient) CheckAuth(token string) (map[string]interface{}, error) {
	authorization := &pb.CheckAuthRequest{Token: token}

	admin, err := sr.remoteUser.CheckAuth(context.Background(), authorization)
	if err != nil {
		return nil, err
	}
	if admin == nil {
		return nil, nil
	}

	return map[string]interface{}{
		"message": admin.Message,
	}, nil
}

func (sr *DemoClient) SetSetting(key string, value string) (map[string]interface{}, error) {
	req := &pb.SetSettingRequest{Key: key, Value: value}

	admin, err := sr.remoteSetting.SetSetting(context.Background(), req)
	if err != nil {
		return nil, err
	}
	if admin == nil {
		return nil, nil
	}

	return map[string]interface{}{
		key: value,
	}, nil
}

func (sr *DemoClient) GetSetting(key string) (map[string]interface{}, error) {
	req := &pb.GetSettingRequest{Key: key}

	resp, err := sr.remoteSetting.GetSetting(context.Background(), req)
	if err != nil {
		return nil, err
	}
	if resp == nil {
		return nil, nil
	}

	return map[string]interface{}{
		key: resp.GetSetting().GetValue(),
	}, nil
}
